﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com/EmlenMud
using System;
using System.ComponentModel;
using BrainTechLLC;
using BrainTechLLC.FilterExtensions;
using System.Drawing;
using System.IO;
using System.Threading;
using BrainTechLLC.WPF;
using System.Diagnostics;
using System.Drawing.Imaging;

namespace ImageAppDataModels
{
    public partial class FileImage : BaseNotifyPropertyChangedUI, INotifyPropertyChanged, IObjectLock, IFileImage, IHasBitmap
    {
        [NonSerialized]
        public string _oldName;
        public static int Instances = 0;

        protected object _lockObject = new object();

        static FileImage()
        {
            ThumbnailProvider = new ThumbnailProvider();
            ThumbnailProvider.SingleThumbnailLoaded += new EventHandler<ObjectEventArgs<IHasBitmap, SingleThumbnail>>(ThumbnailProvider_SingleThumbnailLoaded);
        }

        static void ThumbnailProvider_SingleThumbnailLoaded(object sender, ObjectEventArgs<IHasBitmap, SingleThumbnail> e)
        {
            FileImage fi = e.obj1 as FileImage;
            fi.ThumbnailObject = e.obj2;
        }
        
        public int _originalBitmapWidth, _originalBitmapHeight;
        public PixelFormat _originalBitmapPixelFormat;

        public DateTime CreationTimeNoLoad
        {
            get { return _creationTime; }
            set { _creationTime = value; }
        }

        /// <summary>
        /// The width of the original bitmap
        /// </summary>
        public int OriginalBitmapWidth
        {
            get { return _originalBitmapWidth; }
            set 
            {
                if (_originalBitmapWidth != value)
                {
                    _originalBitmapWidth = value;
                    OnPropertyChanged("OriginalBitmapWidth");
                }
            }
        }

        /// <summary>
        /// The height of the original bitmap
        /// </summary>
        public int OriginalBitmapHeight
        {
            get { return _originalBitmapHeight; }
            set
            {
                if (_originalBitmapHeight != value)
                {
                    _originalBitmapHeight = value;
                    OnPropertyChanged("OriginalBitmapHeight");
                }
            }
        }

        public PixelFormat OriginalBitmapPixelFormat
        {
            get { return _originalBitmapPixelFormat; }
            set
            {
                if (_originalBitmapPixelFormat != value)
                {
                    _originalBitmapPixelFormat = value;
                    OnPropertyChanged("OriginalBitmapPixelFormat");
                }
            }
        }
        object IObjectLock.LockObject { get { return _lockObject; } }

        #region Constructors

        public FileImage()
        {
            Interlocked.Increment(ref Instances);
        }

        ~FileImage()
        {
            _bitmap = null;
            _imageInfo = null;
            _imageSource = null;
            _transformedBitmap = null;
            _imageBytes = null;
            Interlocked.Decrement(ref Instances);
        }

        public FileImage(string fileName = null, FilterCollection filters = null)
            : this()
        {
            this._fileName = fileName;
            this.Filters = filters;
        }

        #endregion

        public override bool Equals(object obj)
        {
            FileImage f = obj as FileImage;
            return (f != null) ? f.FileName == FileName : base.Equals(obj);
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public void MarkEverythingDirty(bool clearThumb)
        {
            MarkEverythingDirty(false, false, !clearThumb);
        }

        public double GetBlur(bool makeCopies)
        {
            return this.BlurMeasure(makeCopies);
        }

        public override string ToString()
        {
            return "FileImage: " + _fileName;
        }
    }
}
